#!/bin/sh
# Start/stop/restart the inotify cron daemon (incrond).

# Sanity check.  If /usr/sbin/incrond is missing then it
# doesn't make much sense to try to run this script:
if [ ! -x /usr/sbin/incrond ]; then
  printf "%s:  no /usr/sbin/incrond found (or not executable); cannot start.\n" "$0"
  exit 1
fi

# Check if incrond is running
incrond_running() {
  ps axc | egrep -q " incrond$"
}

# Start incrond.
incrond_start() {
  incrond_running && {
    echo "incrond is already running."
  } || {
    echo "Starting incrond:  /usr/sbin/incrond"
    /usr/sbin/incrond
  }
}

# Stop incrond (/usr/sbin/incrond):
incrond_stop() {
  incrond_running && {
    echo "Stopping incrond:  /usr/sbin/incrond -k"
    /usr/sbin/incrond -k
  } || {
    echo "incrond is not running."
  }
}

# Restart incrond:
incrond_restart() {
  incrond_stop
  incrond_start
}

case "$1" in
  'start')
    incrond_start
    ;;
  'stop')
    incrond_stop
    ;;
  'restart')
    incrond_restart
    ;;
  'status')
    incrond_running && {
      echo "incrond is running."
    } || {
      echo "No running incrond process."
    }

    ;;
  *)
    printf "usage:\n\t%s start|stop|restart|status\n" "$0"
esac
